// Simple Particle System
// Daniel Shiffman <http://www.shiffman.net>

// A class to describe a group of Particles
// An ArrayList is used to manage the list of Particles 

class ParticleSystem {

  ArrayList<Particle> particles;    // An arraylist for all the particles
  PVector origin;        // An origin point for where particles are birthed
  float  rotation = 0;


  ParticleSystem(int num, PVector v) {
    particles = new ArrayList<Particle>();              // Initialize the arraylist
    origin = v.get();      // Store the origin point
    if (num == 0) return;
    for (int i = 0; i < num; i++) {
      particles.add(generateRandomParticle());    // Add "num" amount of particles to the arraylist
    }
  }

  void run() {
    translate(width/2, height/2);

    Iterator<Particle> it = particles.iterator();

    while (it.hasNext ()) {
      Particle p = it.next();
      if (p.isDead()) {
        it.remove();
      }
      else
      {
        p.run();
      }
    }
  }

  void addParticle() {
    particles.add(generateRandomParticle());
  }

  void addParticle(Particle p) {
    particles.add(p);
  }

  Particle generateRandomParticle()
  {
    return new Particle(origin, new PVector(0, 0.05), new PVector(random(-1, 1), random(-1, 1)), rotation );
  }

  // A method to test if the particle system still has particles
  boolean dead() {
    if (particles.isEmpty()) {
      return true;
    } 
    else {
      return false;
    }
  }
}

